package com.xiaoluyy.demo.api;

import com.xiaoluyy.demo.biz.IAddressBizService;
import com.xiaoluyy.demo.common.CommonBeanUtils;
import com.xiaoluyy.demo.common.domain.po.Address;
import com.xiaoluyy.demo.common.exception.ExceptionCode;
import com.xiaoluyy.demo.common.util.JsonUtil;
import com.xiaoluyy.demo.common.util.ValidateRange;
import com.xiaoluyy.demo.thrift.common.ReturnMsg;
import com.xiaoluyy.demo.thrift.common.Status;
import com.xiaoluyy.common.log.LogFactory;
import com.xiaoluyy.common.log.ILog;
import com.xiaoluyy.demo.thrift.DemoService1;
import com.xiaoluyy.demo.thrift.meta.AddressEntity;
import com.xiaoluyy.demo.thrift.meta.AddressInfo;
import com.xiaoluyy.demo.thrift.meta.AddressReturnMsg;
import com.xiaoluyy.demo.thrift.meta.QueryAddressReturnMsg;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("demoService1")
public class DemoService1Impl implements DemoService1.Iface {

    private static final ILog logger = LogFactory.getLog(DemoService1Impl.class);

    @Autowired
    private IAddressBizService addressBizService;

    @Override
    public ReturnMsg stats() throws TException {
        ReturnMsg returnMsg = new ReturnMsg();
        Status status = new Status();
        status.setCode(ExceptionCode.Ok.getCode());
        status.setMsg("DemoService1 is OK");
        returnMsg.setStatus(status);
        return returnMsg;
    }

    @Override
    public AddressReturnMsg addAddress(String logId, AddressInfo addressInfo) throws TException {
        AddressReturnMsg returnMsg = new AddressReturnMsg();
        Status status = new Status();
        if (addressInfo == null || StringUtils.isBlank(addressInfo.getAddressCode()) || StringUtils.isBlank(addressInfo.getAddressName()) || addressInfo.getAddressType() == 0) {
            status.setCode(ExceptionCode.AddressInfoIllegal.getCode());
            status.setMsg(ExceptionCode.AddressInfoIllegal.getComment());
            returnMsg.setStatus(status);
            return returnMsg;
        }
        if (!ValidateRange.validateRange(addressInfo.getAddressName(), 2, 100)) {
            status.setCode(ExceptionCode.AddressNameLengthIllegal.getCode());
            status.setMsg(ExceptionCode.AddressNameLengthIllegal.getComment());
            returnMsg.setStatus(status);
            return returnMsg;
        }
        Integer id = null;
        try {
            Address address = new Address();
            CommonBeanUtils.normallyCopyBean(address, addressInfo);
            id = this.addressBizService.addAddress(address);
        } catch (Exception e) {
            logger.warn(getClass().getSimpleName() + " addAddress failed,", e);
        }
        if (id != null && id.compareTo(0) > 0) {
            status.setCode(ExceptionCode.Ok.getCode());
            status.setMsg(ExceptionCode.Ok.getComment());
            returnMsg.setStatus(status);
            returnMsg.setAddressId(id);
        } else {
            status.setCode(ExceptionCode.AddressAddFailed.getCode());
            status.setMsg(ExceptionCode.AddressAddFailed.getComment());
            returnMsg.setStatus(status);
        }

        return returnMsg;
    }

    @Override
    public AddressReturnMsg modifyAddress(String logId, int addressId, AddressInfo addressInfo) throws TException {
        AddressReturnMsg returnMsg = new AddressReturnMsg();
        Status status = new Status();
        if (addressId == 0 || addressInfo == null || StringUtils.isBlank(addressInfo.getAddressCode()) || StringUtils.isBlank(addressInfo.getAddressName()) || addressInfo.getAddressType() == 0) {
            status.setCode(ExceptionCode.AddressInfoIllegal.getCode());
            status.setMsg(ExceptionCode.AddressInfoIllegal.getComment());
            returnMsg.setStatus(status);
            return returnMsg;
        }
        if (!ValidateRange.validateRange(addressInfo.getAddressName(), 2, 100)) {
            status.setCode(ExceptionCode.AddressNameLengthIllegal.getCode());
            status.setMsg(ExceptionCode.AddressNameLengthIllegal.getComment());
            returnMsg.setStatus(status);
            return returnMsg;
        }
        Integer id = null;
        try {
            Address address = new Address();
            CommonBeanUtils.normallyCopyBean(address, addressInfo);
            id = this.addressBizService.modifyAddress(addressId, address);
        } catch (Exception e) {
            logger.warn(getClass().getSimpleName() + " modifyAddress failed,", e);
        }
        if (id != null && id.compareTo(1) == 0) {
            status.setCode(ExceptionCode.Ok.getCode());
            status.setMsg(ExceptionCode.Ok.getComment());
            returnMsg.setStatus(status);
            returnMsg.setAddressId(id);
        } else {
            status.setCode(ExceptionCode.AddressModifyFailed.getCode());
            status.setMsg(ExceptionCode.AddressModifyFailed.getComment());
            returnMsg.setStatus(status);
        }

        return returnMsg;
    }

    @Override
    public QueryAddressReturnMsg queryAddress(String logId, int addressId) throws TException {
        QueryAddressReturnMsg returnMsg = new QueryAddressReturnMsg();
        Status status = new Status();

        try {
            Address address = this.addressBizService.queryAddress(addressId);
            AddressEntity addressEntity = new AddressEntity();
            if (address != null) {
                CommonBeanUtils.normallyCopyBean(addressEntity, address);
                addressEntity.setAddressId(address.getId());
                status.setCode(ExceptionCode.Ok.getCode());
                status.setMsg(ExceptionCode.Ok.getComment());
                returnMsg.setStatus(status);
                returnMsg.setAddressEntity(addressEntity);
                logger.info(getClass().getSimpleName() + " queryAddress success");
            }
            else {
                logger.warn(getClass().getSimpleName() + " queryAddress empty");
                status.setCode(ExceptionCode.AddressQueryFailed.getCode());
                status.setMsg(ExceptionCode.AddressQueryFailed.getComment());
                returnMsg.setStatus(status);
            }
            return returnMsg;
        } catch (Exception e) {
            logger.error(getClass().getSimpleName() + " queryAddress failed,", e);
            status.setCode(ExceptionCode.AddressQueryFailed.getCode());
            status.setMsg(ExceptionCode.AddressQueryFailed.getComment());
            returnMsg.setStatus(status);
            return returnMsg;
        }


    }


    @Override
    public AddressReturnMsg deleteAddress(String logId, int addressId) throws TException {
        AddressReturnMsg returnMsg = new AddressReturnMsg();
        Status status = new Status();

        if (addressId == 0) {
            status.setCode(ExceptionCode.AddressInfoIllegal.getCode());
            status.setMsg(ExceptionCode.AddressInfoIllegal.getComment());
            returnMsg.setStatus(status);
            return returnMsg;
        }
        Integer id = null;
        try {
            id = this.addressBizService.deleteAddress(addressId);
        } catch (Exception e) {
            logger.warn(getClass().getSimpleName() + " deleteAddress failed,", e);
        }

        if (id != null && id.compareTo(1) == 0) {
            status.setCode(ExceptionCode.Ok.getCode());
            status.setMsg(ExceptionCode.Ok.getComment());
            returnMsg.setStatus(status);
            returnMsg.setAddressId(addressId);
        } else {
            status.setCode(ExceptionCode.AddressDeleteFailed.getCode());
            status.setMsg(ExceptionCode.AddressDeleteFailed.getComment());
        }


        return returnMsg;
    }


}
